package org.spring.springboot.controller.mall;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.controller.BaseController;
import org.spring.springboot.domain.PageParamVo;
import org.spring.springboot.domain.mall.ShopBaseInfo;
import org.spring.springboot.service.mall.ShopBaseInfoService;
import org.spring.springboot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "Api练手类  商品基本信息", description = "商品基本信息")
@RestController
@RequestMapping(value = "/shop-base-info")
public class ShopBaseInfoController extends BaseController {

    @Autowired
    private ShopBaseInfoService shopBaseInfoService;

    @GetMapping("/test")
    public ResponseResult test(
            @ApiParam(name = "timeFilter", value = "(分页开始数据  默认从第0条开始)", required = true) @RequestParam(defaultValue = "0")
                    int offset,
            @ApiParam(name = "timeFilter", value = "(分页 每页显示多少条 默认50)", required = true) @RequestParam(defaultValue = "50")
                    int limit) {

        return success("a");

    }


    @ApiOperation(value = "全部基本信息", notes = "")
    @GetMapping("/all-info")
    public ResponseResult selectAll(PageParamVo vo) {
        return success(shopBaseInfoService.selectAll(vo));
    }

    @ApiOperation(value = "插入基本信息,支持一条", notes = "")
//    @RequestMapping(value = "update-configuration", method = RequestMethod.POST)
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insert(ShopBaseInfo info) {
        if(!StringUtils.isEmpty(getStrVal(info.getId()))){
            info.setId(null);
        }
        return success(shopBaseInfoService.insert(info));
    }



    @ApiOperation(value = "插入基本信息,支持单条、多条", notes = "")
    @RequestMapping(value = "/insert-batch", method = RequestMethod.POST)
    public ResponseResult insertBatch(@ApiParam(name = "listP", value = "(注意id，新增不用传!)", required = true) @RequestBody List<ShopBaseInfo> listP) {
        if(listP.isEmpty()){
            return error(PARAM_MSG);
        }
       listP.forEach(info -> {
           info.setId(null);
           shopBaseInfoService.insert(info);
       });
        return success();
    }

    @ApiOperation(value = "支持多条、单条删除", notes = "")
    @RequestMapping(value = "/delete-batch", method = RequestMethod.POST)
    public ResponseResult deleteBatch(@ApiParam(name = "listP", value = "(注意id，新增不用传!)", required = true) @RequestBody List<String> ids) {
        if(ids.isEmpty()){
            return error(PARAM_MSG);
        }
        for (int i = 0; i <ids.size() ; i++) {
            String id = ids.get(i);
            if(StringUtils.isEmpty(id)){
                return error(PARAM_MSG);
            }
            shopBaseInfoService.deleteByPrimaryKey(getIntVal(id));
        }
        return success();
    }

    @ApiOperation(value = "删除支持一条", notes = "")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult delete(Integer id) {
        return success(shopBaseInfoService.deleteByPrimaryKey(id));
    }


//    @ApiOperation(value = "修改单条商品", notes = "")
//    @RequestMapping(value = "/update", method = RequestMethod.POST)
//    public ResponseResult update(Integer id) {
//        return success(shopBaseInfoService.);
//    }
//


}
